package Stack;

//数组模拟栈
public class Stack {
    //数据数组
    private int[] data;
    //栈顶序号
    private int top = -1;

    public Stack(int max)
    {
        this.data = new int[max];
    }

    /**
     * 入栈操作
     *
     * @param value int
     *
     * @return void
     *
     * @throws RuntimeException
     **/
    public void push(int value)
    {
        if (this.isFull()) {
            throw new RuntimeException("栈已满");
        }
        this.data[++this.top] = value;
    }

    /**
     * 出栈
     *
     * @return int
     *
     * @throws RuntimeException
     **/
    public int pop()
    {
        if (this.isEmpty()) {
            throw new RuntimeException("栈中不存在数据");
        }
        int value = this.data[this.top];
        this.top--;
        return value;
    }

    /**
     * 获取栈中的数据数量
     *
     * @return int
     **/
    public int count()
    {
        return this.top + 1;
    }

    /**
     * 栈是否满
     *
     * @return boolean
     **/
    public boolean isFull()
    {
        return this.top == (this.data.length - 1);
    }

    /**
     * 栈是否空
     *
     * @return boolean
     **/
    public boolean isEmpty()
    {
        return this.top == -1;
    }
}
